/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities'),
		id = sessionStorage.id;

	var app = new Vue({
		el: '#uc_work',
		data: {
			req: {
				company: '',
				major: '',
				work: '',
				companyType: '',
				income: '',
				language: ''
			}
		},
		methods: {
			init: function () {
				app.detail = utilities.base64TojsonObj(sessionStorage.userInfo).detail;

				for (var key in app.req) {
					app.req[key] = app.detail[key];
				}

				_(Config.DICTIONARY, {
					parentId: '572,605,615,625,632'
				}, function (data) {
					for(var key in data.obj){
						data.obj[key].unshift({id: '', name: '请选择'});
					}

					data.obj[632].splice(0,1);

					app.$set('majorList', data.obj[572]);
					app.$set('workList', data.obj[605]);
					app.$set('companyTypeList', data.obj[615]);
					app.$set('incomeList', data.obj[625]);

					var languageList = data.obj[632];
					utilities.restoreCheckList(languageList, app.req.language.split(','));

					app.$set('languageList', languageList);
				});
			},
			save: function () {
				var req = {};
				for (var key in app.req) {
					req['xgdetail.' + key] = app.req[key];
				}

				req['xgdetail.language'] = utilities.getCheckedStr(app.languageList);

				req.id = id;
				console.log(req);

				_(Config.SERVICE.USERCENTER.WORK_SAVE, req, function (data) {
					if (data.code == 200 && data.type === 'SUCCESS') {
						alert('保存成功！');
						utilities.refreshUserInfo(function (data) {
							history.back();
						});
					} else {
						alert(data.errmsg);
					}
				});
			},
			isChecked: function (item) {
				return !!item.checked;
			},
			pick: function (i) {
				app.$set('languageList[' + i + '].checked', !app.languageList[i].checked);
			},
			getName: function (arr, id) {
				for (var i = 0; i < arr.length; i++)
					if (arr[i]['id'] == id)
						return arr[i]['name'];
			}
		}
	});
	app.init();
});
